<!DOCTYPE html>
<html>

<head lang="zh-cmn-Hans">
    <meta charset="UTF-8">
    <title>DSBridge Test</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=0.5,user-scalable=no"/>
    <!--require dsbridge init js-->
    <script src="https://cdn.jsdelivr.net/npm/dsbridge/dist/dsbridge.js"></script>
    <script src="./js/zepto.min.js"></script>
</head>
<style>
    html,
    body {
        padding: 0;
        margin: 0;
    }

    * {
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
    }


    .btn {
        text-align: center;
        background: #5FADEF;
        color: #fff;
        padding: 20px;
        margin: 30px;
        font-size: 24px;
        border-radius: 6px;
        box-shadow: 4px 2px 10px #999;
    }

    .top-bar {
        text-align: center;
        background: #5FADEF;
        color: #fff;
        line-height: 100px;
        height: 100px;
        font-size: 28px;
    }

    .line-bar {
        text-align: center;
        color: #333;
        line-height: 100px;
        height: 100px;
        font-size: 28px;
    }


    .btn:active {
        opacity: .7;
        box-shadow: 4px 2px 10px #555;
    }
</style>

<body>

<div class="top-bar">测试样例</div>


<div class="line-bar">系统功能</div>
<div class="btn" onclick="openDeviceManager()">打开设备管理界面</div>
<div class="btn" onclick="openTargetPage('/module_weight/device_manager')">打开指定界面(平衡仪)</div>
<div class="btn" onclick="openTargetPage('/module_brain/device_manager')">打开指定界面(脑波仪)</div>
<div class="btn" onclick="openTargetPage('/module_fat/device_manager')">打开指定界面(体脂仪)</div>
<div class="btn" onclick="screenshot()">截图</div>
<div class="btn" onclick="toast()">弹出消息</div>
<div class="btn"><a href="https://www.baidu.com" style="color: #fff;">原页面打开网页</a></div>
<div class="btn"><a href="https://www.baidu.com" style="color: #fff;" target="_blank">新建标签打开网页</a></div>


<div class="line-bar">课程功能</div>
<div class="btn" onclick="complete()">结束当前课节</div>

<div class="line-bar">脑波功能</div>
<div class="btn" onclick="brainIsConnected()">脑波是否连接</div>
<div class="btn" onclick="brainBtart()">脑波开始</div>
<div class="btn" onclick="brainStop()">脑波停止</div>
<div style="margin: 30px;   font-size: 24px;color: #5FADEF;"><span>当前脑波值：</span><span id="tv_val"
                                                                                      style="color: red;">0</span>
</div>


<div class="line-bar">脉冲功能</div>
<div class="btn" onclick="pulseIsConnected()">脉冲是否连接</div>
<div class="btn" onclick="pulseStart()">脉冲开始</div>
<div class="btn" onclick="pulseStop()">脉冲停止</div>


<div class="line-bar">平衡仪功能</div>
<div class="btn" onclick="weightIsConnected()">平衡仪是否连接</div>
<div class="btn" onclick="weightStart()">平衡仪开始</div>
<div class="btn" onclick="weightStop()">平衡仪停止</div>
<div class="btn" onclick="onPointChange(100,200)">获取重心坐标</div>
<div style="margin: 30px;   font-size: 24px;color: #5FADEF;"><span>当前重心坐标：</span><span id="tv_point_val"
                                                                                       style="color: red;">0,0</span>


    <div class="line-bar">体脂仪功能</div>
    <div class="btn" onclick="fatIsConnected()">体脂仪是否连接</div>
    <div class="btn" onclick="fatStart(1)">部分测量</div>
    <div class="btn" onclick="fatStart(2)">快速测量</div>
    <div class="btn" onclick="fatStart(3)">全身测量</div>

</div>


<script>


    /**   脑波功能  START **/

    var id = -1

    function brainIsConnected() {
        alert("脑波是否连接 = " + dsBridge.call("brain.isConnected", "function is connected"))
    }

    function brainBtart() {
        dsBridge.call("brain.start", "function start")
        id = setInterval(fetchBrainValue, 1000)
    }

    function brainStop() {
        dsBridge.call("brain.stop", "function stop")
        clearInterval(id)
    }

    function fetchBrainValue() {
        var val = dsBridge.call("brain.fetchValue", "function fetchValue")
        document.getElementById("tv_val").textContent = val
    }

    /**   脑波功能  END **/


    /**   系统功能  START **/
    function toast() {
        dsBridge.call("system.showToast", "this is a simple message")
    }

    function screenshot() {
        dsBridge.call("system.screenshot", "this is a simple message", function (path) {
            alert(path)
        })
    }

    function openDeviceManager() {
        dsBridge.call("system.deviceManager", "function is openDeviceManager")
    }

    function openTargetPage(path) {
        dsBridge.call("system.openTargetPage", path)
    }

    /**   脑波功能  END **/




    function complete() {
        dsBridge.call("course.complete", "function is openDeviceManager")
    }


    /**   脉冲功能  START **/
    function pulseIsConnected() {
        alert("脉冲是否连接 = " + dsBridge.call("pulse.isConnected", "testSyn"))
    }

    function pulseStart() {
        dsBridge.call("pulse.start", "080701010580")
    }

    function pulseStop() {
        dsBridge.call("pulse.stop", "080700000080")
    }

    /**   脉冲功能  END **/


    /**   平衡仪功能  START **/
    function weightIsConnected() {
        alert("平衡仪是否连接 = " + dsBridge.call("weight.isConnected", "testSyn"))
    }

    function weightStart() {
        dsBridge.call("weight.start", "080701010580")
    }

    function weightStop() {
        dsBridge.call("weight.stop", "080700000080")
    }


    //测量结束回调
    function onGaugeEnd() {
        alert("平衡仪测量结束")
    }

    //平衡称断开回调
    function onWeightDisconnect() {
        alert("平衡仪断开连接")
    }

    //重心改变回调
    function onPointChange(x, y) {
        $("#tv_point_val")[0].innerText = x + "," + y
    }


    dsBridge.register("weight", {
        tag: "weight",
        onGaugeEnd: onGaugeEnd,
        onWeightDisconnect: onWeightDisconnect,
        onPointChange: onPointChange
    })


    /**   脉冲功能  END **/


    /**   体脂仪功能  START **/

    var userInfo = {
        name: "eenie",
        sex: 1,
        birthday: "2020-06-16"
    }


    function fatIsConnected() {
        alert("体脂仪是否连接 = " + dsBridge.call("fat.isConnected", "fat"))
    }


    function fatStart(type) {
        userInfo.type = type;
        dsBridge.call("fat.start", JSON.stringify(userInfo))
    }


    function onFatGaugeEnd(result) {
        alert("体脂仪测量结束\n" + result)
    }

    dsBridge.register("fat", {
        tag: "fat",
        onFatGaugeEnd: onFatGaugeEnd,
    })


</script>
</body>

</html>
